package vn.allmobile.newsreader.adapters;

import java.util.ArrayList;
import java.util.List;

import vn.allmobile.newsreader.R;
import vn.allmobile.newsreader.data.Paper;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

public class AllPapersAdapter extends BaseExpandableListAdapter {
	List<Paper> mPapers;
	Context mContext;
	@SuppressWarnings("rawtypes")
	private List mKeysCategories;

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public AllPapersAdapter(Context context, List<Paper> listPapers) {
		this.mContext = context;
		this.mPapers = listPapers;
		mKeysCategories = new ArrayList<List>();
		for (int i = 0; i < this.mPapers.size(); i++) {
			mKeysCategories.add(new ArrayList(this.mPapers.get(i)
					.getCategories().keySet()));
		}
	}

	@Override
	public Object getChild(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		final int mGroupPosition = groupPosition;
		ChildHolder mChildHoder;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(
					vn.allmobile.newsreader.R.layout.allpapers_list_group_item,
					null);
			mChildHoder = new ChildHolder();
			mChildHoder.title = (TextView) convertView
					.findViewById(R.id.allpapers_item_title);
			mChildHoder.checkBox = (CheckBox) convertView
					.findViewById(R.id.allpapers_chkCheckCategory);
			convertView.setTag(mChildHoder);
		} else {
			mChildHoder = (ChildHolder) convertView.getTag();
		}
		final Object mKey = ((ArrayList) this.mKeysCategories
				.get(groupPosition)).get(childPosition);
		String mTitle = this.mPapers.get(groupPosition).getCategories()
				.get(mKey).getTitle();
		final boolean mIsEnable = this.mPapers.get(groupPosition)
				.getCategories().get(mKey).isEnable();

		mChildHoder.title.setText(mTitle);
		mChildHoder.checkBox.setChecked(mIsEnable);
		mChildHoder.checkBox.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mPapers.get(mGroupPosition).getCategories().get(mKey)
						.setEnable(!mIsEnable);
				if (mIsEnable == false) {// False => True
					mPapers.get(mGroupPosition).setEnable(true);
				} else {
					// Check others child, if all is unable => unable group
					boolean mIsAllUnable = true;
					for (Object mKey : mPapers.get(mGroupPosition)
							.getCategories().keySet()) {
						if (mPapers.get(mGroupPosition).getCategories()
								.get(mKey).isEnable()) {
							mIsAllUnable = false;
							break;
						}
					}
					if (mIsAllUnable) {
						mPapers.get(mGroupPosition).setEnable(false);
					}
				}
				notifyDataSetChanged();
			}
		});
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return this.mPapers.get(groupPosition).getCategories().size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return mPapers.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		final int mGroupPosition = groupPosition;
		final boolean mIsExpanded = isExpanded;
		GroupHolder mGroupHoder;
		if (convertView == null) {
			convertView = LayoutInflater
					.from(mContext)
					.inflate(
							vn.allmobile.newsreader.R.layout.allpapers_list_group,
							null);
			mGroupHoder = new GroupHolder();
			mGroupHoder.title = (TextView) convertView
					.findViewById(vn.allmobile.newsreader.R.id.allpapers_group_title);
			mGroupHoder.checkBox = (CheckBox) convertView
					.findViewById(vn.allmobile.newsreader.R.id.allpapers_chkCheckPaper);
			convertView.setTag(mGroupHoder);
		} else {
			mGroupHoder = (GroupHolder) convertView.getTag();
		}

		String mGroupTitle = this.mPapers.get(groupPosition).getTitle();
		final boolean mIsEnable = this.mPapers.get(groupPosition).isEnable();
		// Set
		mGroupHoder.title.setText(mGroupTitle);
		mGroupHoder.checkBox.setChecked(mIsEnable);
		mGroupHoder.checkBox.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mPapers.get(mGroupPosition).setEnable(!mIsEnable);
				// Set for child
				List<Object> mKeyList = new ArrayList(mPapers
						.get(mGroupPosition).getCategories().keySet());
				for (Object mKey : mKeyList) {
					mPapers.get(mGroupPosition).getCategories().get(mKey)
							.setEnable(!mIsEnable);
				}
				if (mIsExpanded) {
					notifyDataSetChanged();
				}
			}
		});
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return false;
	}

	// //////////////////////////////////////////////////////////////////////
	class GroupHolder {
		TextView title;
		CheckBox checkBox;
	}

	class ChildHolder {
		TextView title;
		CheckBox checkBox;
	}

}
